package cn.founder.org.controller;

import cn.founder.basic.util.AjaxResult;
import cn.founder.basic.util.PageList;
import cn.founder.org.domain.Dictidetai;
import cn.founder.org.query.DictidetaiQuery;
import cn.founder.org.service.IDictidetaiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictidetai")
public class DictidetaiController {

    @Autowired
    private IDictidetaiService dictidetaiService;


    //查询一条数据
    @GetMapping("/{id}")
    public Dictidetai queryOne(@PathVariable("id") Long id){
        return dictidetaiService.queeryById(id);
    }

    //查询所有
    @GetMapping
    public List<Dictidetai> queryAll(){
        return dictidetaiService.queryAll();
    }

    //删除一条数据
    @DeleteMapping("/{id}")
    public AjaxResult delOne(@PathVariable("id") Long id){
        try {
            dictidetaiService.del(id);
            return AjaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败");
        }
    }

    //添加或修改
    @PutMapping
    public AjaxResult addorUpdata(@RequestBody Dictidetai dictidetai){
        try {
            if (dictidetai.getId()==null){
                dictidetaiService.add(dictidetai);
            }else {
                dictidetaiService.update(dictidetai);
            }
            return  AjaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.fail("操作失败");
        }
    }

    //分页查询
    @PostMapping
    public PageList<Dictidetai> queryPage(@RequestBody DictidetaiQuery query){
        return dictidetaiService.queryPage(query);
    }



    //批量删除一条数据
    @PatchMapping
    public AjaxResult pathchDelete(@RequestBody List<Long> ids){
        try {
            dictidetaiService.pathchDel(ids);
            return AjaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败");
        }
    }

}
